// pages/admin/settings/settings.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noticeForm: {
      title: '',
      content: ''
    },
    noticeList: [
      {
        id: 1,
        title: '系统维护通知',
        content: '系统将于本周六凌晨2点进行例行维护，预计维护时间2小时。',
        time: '2024-01-20 10:30'
      },
      {
        id: 2,
        title: '新功能上线通知',
        content: '新版本已更新，增加了数据统计功能，欢迎体验。',
        time: '2024-01-19 15:20'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 加载设置
    this.loadSettings()
  },

  loadSettings() {
    // 这里可以从本地存储或服务器获取设置
    const settings = wx.getStorageSync('systemSettings')
    if (settings) {
      this.setData({
        settings
      })
    }
  },

  toggleNotification(e) {
    const value = e.detail ? e.detail.value : !this.data.settings.notification
    this.setData({
      'settings.notification': value
    })
    this.saveSettings()
  },

  toggleAutoBackup(e) {
    const value = e.detail ? e.detail.value : !this.data.settings.autoBackup
    this.setData({
      'settings.autoBackup': value
    })
    this.saveSettings()
  },

  saveSettings() {
    // 保存设置到本地存储
    wx.setStorageSync('systemSettings', this.data.settings)
    wx.showToast({
      title: '设置已保存',
      icon: 'success'
    })
  },

  clearCache() {
    wx.showModal({
      title: '清除缓存',
      content: '确定要清除缓存数据吗？',
      success: (res) => {
        if (res.confirm) {
          // 执行清除缓存操作
          wx.showLoading({
            title: '清理中...'
          })
          
          setTimeout(() => {
            wx.hideLoading()
            this.setData({
              cacheSize: '0B'
            })
            wx.showToast({
              title: '清除成功',
              icon: 'success'
            })
          }, 1500)
        }
      }
    })
  },

  backupData() {
    wx.showLoading({
      title: '备份中...'
    })
    
    // 这里添加数据备份逻辑
    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '备份成功',
        icon: 'success'
      })
    }, 2000)
  },

  checkUpdate() {
    wx.showLoading({
      title: '检查更新...'
    })
    
    // 这里添加检查更新逻辑
    setTimeout(() => {
      wx.hideLoading()
      wx.showModal({
        title: '检查更新',
        content: '当前已是最新版本',
        showCancel: false
      })
    }, 1500)
  },

  onInputChange(e) {
    const field = e.currentTarget.dataset.field
    this.setData({
      [`noticeForm.${field}`]: e.detail.value
    })
  },

  submitNotice() {
    const { title, content } = this.data.noticeForm
    if (!title.trim() || !content.trim()) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      })
      return
    }

    // 这里应该调用发布公告的接口
    const newNotice = {
      id: Date.now(),
      title: title,
      content: content,
      time: this.formatTime(new Date())
    }

    this.setData({
      noticeList: [newNotice, ...this.data.noticeList],
      noticeForm: {
        title: '',
        content: ''
      }
    })

    wx.showToast({
      title: '发布成功',
      icon: 'success'
    })
  },

  deleteNotice(e) {
    const id = e.currentTarget.dataset.id
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这条公告吗？',
      success: (res) => {
        if (res.confirm) {
          // 这里应该调用删除公告的接口
          const newList = this.data.noticeList.filter(item => item.id !== id)
          this.setData({
            noticeList: newList
          })
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          })
        }
      }
    })
  },

  formatTime(date) {
    const year = date.getFullYear()
    const month = (date.getMonth() + 1).toString().padStart(2, '0')
    const day = date.getDate().toString().padStart(2, '0')
    const hour = date.getHours().toString().padStart(2, '0')
    const minute = date.getMinutes().toString().padStart(2, '0')
    return `${year}-${month}-${day} ${hour}:${minute}`
  }
})